import Home from './home-model.js';
import config from '../utils/config.js'
const homeModel = new Home();
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    catList: [],
    banner: [],
    news: [],
    page: 1,
    size: 10,
    more: false,
    current: -1,
    moreText: '不能在拉了，到底了！'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this._loadData();
  },
  _loadData() {
    homeModel.getCatData((res) => {
      this.setData({
        catList: res
      })
    });
    this.getList(true, this.data.current);
  },
  onCat(e) {
    const index = homeModel.getDataSet(e, 'index');
    this.setData({
      current: index
    })
    if (index === 0) {
      this.getPositions(true);
      return;
    }
    this.getList(true, index);
  },
  getPositions(init) {
    if (init) {
      this.setData({
        page: 1,
        more: true
      })
    }
    homeModel.getPositions(this.data.page, this.data.size, (res) => {
      res.map((item, index) => {
        item.image = config.imgBaseUrl + item.image;
      })
      if (res.length < 1) {
        this.setData({
          more: false
        })
        return;
      }
      if (init) {
        this.setData({
          more: true,
          news: res
        })
      } else {
        this.setData({
          more: true,
          news: this.data.news.concat(res)
        })
      }
    })
  },
  onNewsDetail(e) {
    var id = homeModel.getDataSet(e, 'index');
    wx.navigateTo({
      url: '../newsDetail/newsDetail?id=' + id,
    });
  },
  getList(init, catid) {
    var that = this;
    if (init) {
      this.setData({
        page: 1,
        more: true
      })
    }
    homeModel.getIndexData(this.data.page, this.data.size, catid, (res) => {
      res.heads.map((item, index) => {
        item.image = config.imgBaseUrl + item.image;
      })
      res.news.list.map((item, index) => {
        item.image = config.imgBaseUrl + item.image;
        item.create_time = item.create_time.replace(item.create_time.substring(item.create_time.indexOf(' ') + 1, item.create_time.length), '')
      })
      if (res.news.list.length < 1) {
        that.setData({
          more: false
        })
        return;
      }
      if (init) {
        if (this.data.current === -1) {
          this.setData({
            more: true,
            banner: res.heads,
            news: res.news.list
          })
        } else {
          this.setData({
            more: true,
            banner: [''],
            news: res.news.list
          })
        }
      } else {
        if (this.data.current === -1) {
          this.setData({
            more: true,
            news: this.data.news.concat(res.news.list)
          })
        } else {
          this.setData({
            more: true,
            banner: [''],
            news: this.data.news.concat(res.news.list)
          })
        }
      }
    })

  },
  //搜索
  onSearch() {
    wx.navigateTo({
      url: '../search/search',
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    if (this.data.current === 0) {
      this.getPositions(true);
      return;
    }
    this.getList(true, this.data.current);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (!this.data.more) {
      return false
    }
    this.setData({
      page: this.data.page + 1
    })
    if (this.data.current === 0) {
      this.getPositions(false);
      return;
    }
    this.getList(false, this.data.current);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})